<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Template
 *
 * @category   	Gnn Cms
 * @package    	Gnn View
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 Jun 1, 2013 || 9:49:13 AM
 */
class Zendvn_Template {
	public  $templatePath;
	public  $templateUrl;
	public  $dbInfo;
	
	private $_configFileName;
	private $_configSection;
	public  $configInfo;
	public  $gcView;
	
	public  $content;
	
	public function  __construct($client_id,$template_id, $view,$render = true ,$configFileName = "loader.ini", $configSection = "default"){
		$this->dbInfo 		   = $this->loadTemplateInfo($client_id,$template_id);
		$this->templatePath	   = ($client_id == 1) ? TEMPLATE_PATH_FRONT.DS.$this->dbInfo->template 
												   : TEMPLATE_PATH_BACK .DS.$this->dbInfo->template;
		
		$this->_configFileName = $configFileName;
		$this->_configSection  = $configSection;
		$this->gcView		   = $view;
		$this->_configLoader();
		
		if($render) $this->content 		   = $this->renderTemplate();
	}
	
	/**
	 * Load template info
	 * @param integer $client_id
	 * @return mixed
	 */
	public function loadHomeTemplate($client_id = 1){
		$dbo 		= Zend_Registry::get(REGISTRY_KEY_DBO);
		$sql 		= $dbo  ->select()
							->from(TBL_PRF.'template_styles',array('template','title','params','client_id'))
							->where('home = 1')
							->where('client_id = ?',$client_id);
		$template = current($dbo->fetchAll($sql));
	
		return (object)$template;
	}
	
	/**
	 * Load template info
	 * @param integer $client_id
	 * @param integer $template_id
	 * @return mixed
	 */
	public function loadTemplateInfo($client_id = 1, $template_id){
		$dbo 		= Zend_Registry::get(REGISTRY_KEY_DBO);
		$sql 		= $dbo  ->select()
							->from(TBL_PRF.'template_styles',array('template','title','params','client_id'))
							->where('client_id = ?',$client_id);
		// neu nhu page khong chi dinh dung template nao thi
		// chung ta su dung template home
		if($template_id != 0){
			$sql->where('id = ?',$template_id);
		}else{
			$sql->where('home = 1');
		}
		$template = current($dbo->fetchAll($sql));
		return (object)$template;
	}
	
	/**
	 * load configuration from config file
	 */
	private function _configLoader(){
		$filename 				= $this->templatePath . DS . $this->_configFileName;
		$this->configInfo 		= new Zend_Config_Ini($filename,$this->_configSection);
		$this->templateUrl 		= $this->configInfo->templateUrl;
		$this->loadScript();
	}
	/**
	 * clear headtitle
	 * clear headmeta
	 * clear headLink
	 * clear headScript
	 * @param unknown_type $view
	 */
	public static function clearLayoutHeadInfo($view){
		$view->headTitle()->set('');
		$view->headMeta()->getContainer()->exchangeArray(array());
		$view->headLink()->getContainer()->exchangeArray(array());
		$view->headScript()->getContainer()->exchangeArray(array());
	}
	
	/**
	 * load template header information
	 */
	private function loadTemplateHeadInfo(){
		// set title
		// $this->view->headTitle($this->configInfo->templateTitle);
		// set meta tag
		if(count($this->configInfo->metaHttp)>0){
			foreach ($this->configInfo->metaHttp as $key => $value){
				$tmp = explode("||",$value);
				$this->gcView->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
			}
		}
		if(count($this->configInfo->metaName)>0){
			foreach ($this->configInfo->metaName as $key => $value){
				$tmp = explode("||",$value);
				$this->gcView->headMeta()->appendName($tmp[0],$tmp[1]);
			}
		}
	}
	
	/**
	 * load template scripts, stylesheet 
	 */
	public function loadScript(){
		
		//load template head
		$this->loadTemplateHeadInfo();
		//load stylesheet files
		if(count($this->configInfo->cssDir) > 0) foreach ($this->configInfo->cssDir as $cssDir){
			if(count($this->configInfo->$cssDir) >0) foreach ($this->configInfo->$cssDir as $cssFile){
				$cssUrl = $this->templateUrl.DS.$cssDir;
				if($cssDir == "rootCss") $cssUrl = $this->templateUrl;
				$this->gcView->headLink()->appendStylesheet($cssUrl. DS . $cssFile,'screen');
			}
		}
		//load javascript files
		if(count($this->configInfo->jsDir) > 0) foreach ($this->configInfo->jsDir as $jsDir){
			if(count($this->configInfo->$jsDir) >0) foreach ($this->configInfo->$jsDir as $jsFile){
				$jsUrl = $this->templateUrl.DS.$jsDir;
				if($jsDir == "rootJs") $jsUrl = $this->templateUrl;
				$this->gcView->headScript()->appendFile($jsUrl .DS. $jsFile,'text/javascript');
			}
		}
	}
	
	/**
	 * render template into string
	 */
	public function renderTemplate($templateRoot = "index.php"){
		$xhtml = Zendvn_View_RenderHelper::render($this->templatePath.DS.$templateRoot, $this->gcView);
		if (count ( $this->configInfo->replace ) > 0) {
			foreach ( $this->configInfo->replace as  $val ) {
				if ($val == 'css') {
					$xhtml = str_replace ( 'href="'.$val, 'href="' . $this->templateUrl.DS.$val, $xhtml );
				} else if($val == 'js'){
					$xhtml = str_replace ( 'src="' . $val, 'src="' . $this->templateUrl.DS.$val, $xhtml );
				}
				else if($val == 'images'){
					$xhtml = str_replace ( 'src="' . $val, 'src="' . $this->templateUrl.DS.$val, $xhtml );
				}
			}
		}
		return $xhtml;
	}
}
